package org.polaris.gift.exception;

import cn.hutool.core.util.StrUtil;
import org.polaris.gift.constants.ResponseCode;

import java.util.Collection;
import java.util.Objects;

/*
* 断言工具类
* */
public class PolarisAssert {
    //断言字符串为空
    //isBlank认为"    "是一个空串,
    //isEmpty认为"    "不是一个空串
    public static void isBlank(String str,String message){
        if (StrUtil.isNotBlank(str))
            throw new BusinessException(message);
    }
    //断言字符串不为空
    public static void isNotBlank(String str,String message){
        if (StrUtil.isBlank(str))
            throw new BusinessException(message);
    }
    // 断言对象为空
    public static void isNull(Object obj,String message){
        // 如果对象不为空，就抛出异常
        if(Objects.nonNull(obj)){
            throw new BusinessException(message);
        }
    }

    // 断言对象不为空
    public static void isNotNull(Object obj,String message){
        // 如果对象为空，就抛出异常
        if(Objects.isNull(obj)){
            throw new BusinessException(message);
        }
    }

    // 断言集合为空
    public static void isNull(Collection collection, String message){
        // 如果集合不为空，就抛出异常
        if(collection != null && collection.size() > 0){
            throw new BusinessException(message);
        }
    }

    // 断言集合不为空
    public static void isNotNull(Collection collection, String message){
        // 如果集合为空，就抛出异常
        if(collection == null || collection.size() == 0){
            throw new BusinessException(message);
        }
    }

    // 断言两个字符串值相等
    public static void isEq(String str1,String str2,String message){
        // 如果两个字符串不相等，就抛出异常
        if(!str1.equals(str2)){
            throw new BusinessException(message);
        }
    }
    //====================================重载=========================================================
    public static void isBlank(String str, ResponseCode code){
        if (StrUtil.isNotBlank(str))
            throw new BusinessException(code);
    }
    //断言字符串不为空
    public static void isNotBlank(String str,ResponseCode code){
        if (StrUtil.isBlank(str))
            throw new BusinessException(code);
    }
    // 断言对象为空
    public static void isNull(Object obj,ResponseCode code){
        // 如果对象不为空，就抛出异常
        if(Objects.nonNull(obj)){
            throw new BusinessException(code);
        }
    }

    // 断言对象不为空
    public static void isNotNull(Object obj,ResponseCode code){
        // 如果对象为空，就抛出异常
        if(Objects.isNull(obj)){
            throw new BusinessException(code);
        }
    }

    // 断言集合为空
    public static void isNull(Collection collection, ResponseCode code){
        // 如果集合不为空，就抛出异常
        if(collection != null && collection.size() > 0){
            throw new BusinessException(code);
        }
    }

    // 断言集合不为空
    public static void isNotNull(Collection collection, ResponseCode code){
        // 如果集合为空，就抛出异常
        if(collection == null || collection.size() == 0){
            throw new BusinessException(code);
        }
    }

    // 断言两个字符串值相等
    public static void isEq(String str1,String str2,ResponseCode code){
        // 如果两个字符串不相等，就抛出异常
        if(!str1.equals(str2)){
            throw new BusinessException(code);
        }
    }

    /**
     * 断言字符串相同,不区分大小写
     * @param redisVerifyCode
     * @param inputVerifyCode
     * @param code
     */
    public static void isEqIgnoreCase(String redisVerifyCode, String inputVerifyCode,
                                      ResponseCode code) {
        if (!redisVerifyCode.equalsIgnoreCase(inputVerifyCode))
            throw new BusinessException(code.getMessage(),code.getCode());
    }
}
